﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Helpers;
using System.Xml.Linq;
using System.Diagnostics;
using Constants;
using Eneta.Portal.Common.Definitions;

namespace Eneta.Portal.Literature.Controls
{
    public class LiteratureCommentAddControl : UserControl
    {
        protected HtmlAnchor currentUserProfileLink;
        protected HtmlAnchor currentUserProfileLinkWithName;
        protected HtmlImage currentUserImage;
        protected TextBox commentArea;
        protected Button addComment;

        public string WebUrl { get; set; }
        public string ListName { get; set;  }

        public int ItemId { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.addComment.Click += new EventHandler(addComment_Click);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (SPContext.Current.Web.CurrentUser == null)
            {
                this.Visible = false;
                return;
            }
            else
            {
                Logger.LogInfo("Raamatu detailvaatel kommenteerimise kasutaja puudu");
            }

            BindUserInfo();
        }

        private void BindUserInfo()
        {
            var profileRepository = new UserProfileRepository(SPContext.Current.Web);
            var user = SPContext.Current.Site.RootWeb.CurrentUser;
            var profile = profileRepository.GetUserProfileByProfileName(user.Name);
            var profileUrl = UrlHelper.GetProfileUrl(profile.Id);
            var profileImageUrl = UrlHelper.GetUserImageHandlerUrl(16, profile.Id);


            currentUserProfileLink.HRef = profileUrl;
            currentUserProfileLinkWithName.HRef = profileUrl;
            currentUserProfileLinkWithName.InnerText = SPContext.Current.Site.RootWeb.CurrentUser.Name;
            currentUserImage.Src = profileImageUrl;
            currentUserImage.Alt = SPContext.Current.Site.RootWeb.CurrentUser.Name;
        }

        void addComment_Click(object sender, EventArgs e)
        {
            //TODO: Move to repository
            if (string.IsNullOrEmpty(this.commentArea.Text))
                return;
            if (ItemId == 0)
                return;
            var currentWeb = SPContext.Current.Web;
            var repository = new LiteratureRepository(currentWeb);
            var currentItem = repository.GetLiteratureItemById(ItemId);
            var siteId = SPContext.Current.Site.ID;
            var userName = SPContext.Current.Web.CurrentUser.LoginName;
           

            var sec = new SPSecurity.CodeToRunElevated(delegate
            {
                using (var site = new SPSite(siteId))
                using (var web = site.OpenWeb(WebUrl))
                {
                    site.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;
                    var list = web.Lists[ListName];
                    var newItem = list.Items.Add();
                    newItem[LiteratureCommentFields.Comment] = commentArea.Text;
                    var listInfo = currentWeb.ServerRelativeUrl + ";" + "Lehed";
                    newItem[LiteratureCommentFields.List] = listInfo;
                    newItem[LiteratureCommentFields.ItemId] = currentItem.ID;
                    var currentPoster = web.SiteUsers[userName];
                    if (currentPoster != null)
                    {
                        newItem[Fields.RealAuthor.Name] = currentPoster;
                        newItem[SPFields.Author] = currentPoster;
                    }
                    newItem.Update();
                    list.Update();
                    site.AllowUnsafeUpdates = false;
                    web.AllowUnsafeUpdates = false;
                }
            });
            SPSecurity.RunWithElevatedPrivileges(sec);
            Response.Redirect(Request.RawUrl, true);
        }
    }
}
